from unstructured.partition.auto import partition
from unstructured.partition.utils.constants import PartitionStrategy 
import chromadb
from chromadb.config import Settings
import os

def feedChroma(name:str,file_path:str):

    # print(f"pdf file is located at {file_path}")
    # client = chromadb.HttpClient(host="localhost",port=8000)

    dbpath = os.path.join(os.path.dirname(__file__),"..","..",".db")
    client = chromadb.PersistentClient(path=dbpath,settings=Settings(anonymized_telemetry=False))
    
    collection_name = name if (name  == '' or name == None) == False  else  file_path
    print(f"collection name is {collection_name}")
    
    collection = client.get_or_create_collection(name=name)

    if(collection.count() == 0):

        collection = client.get_or_create_collection(name=name)

        elements = partition(filename=file_path,strategy=PartitionStrategy.AUTO,starting_page_number=0,)

        ids=[]
        docs=[]
        
        for element in elements:
            print(f"element-{element.id},text:\n{element.text}\n\n")
            ids.append(element.id)
            docs.append(element.text)

        collection.add(ids=ids,documents=docs)

        print("end process\n\n")

    return collection